﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FastPoster.MainClasses
{
    #region USING
    using FastPoster.Helpers;
    using FastPoster.MainForms;
    #endregion

    public class Poster
    {
        public string           BoxLink { get; set; }
        public bool             Completed { get; set; }
        public int              IDB { get; set; }
        public int              IDM { get; set; }
        public int              IDP { get; set; }
        public string           Mess { get; set; }
        public string           Pass { get; set; }
        public DateTime         PostedTime { get; set; }
        public string           ResponseUrl { get; set; }
        public string           Subj { get; set; }
        public string           ThreadLink { get; set; }
        public string           Url { get; set; }
        public string           User { get; set; }
        public EventHandler     ProgressChanged { get; set; }
        public bool             LoginSuccess { get; set; }
		public bool             VerifyCapcha { get; set; }
        public string           StrRespond { get; set; }
        public string           HumanVerification { get; set; }
		public bool             PostFinish{ get; set; }
        public bool             IsDeadBox { get; set; }
        public int              IDBaiVietDaDang { get; set; }
        public DateTime         NgayDang { get; set; }

        protected void OnProgressChanged(ProgressChangedArgs e)
        {
            if (this.ProgressChanged != null)
            {
                this.ProgressChanged(this, e);
            }
        }

        public object TestForum(int index)
        {
            this.PostFinish = false;
            this.OnProgressChanged(new ProgressChangedArgs(index, "Bắt đầu đăng nhập..."));
            VBB vbb = new VBB(this.Url);
            vbb.ProgressChanged += vbb_OnProcess;
            vbb.Index = index;
            if (vbb.Login(this.User, this.Pass))
            {
                this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập thành công."));
                this.ResponseUrl = vbb.ResponseUrl;
            }
            else
            {
                this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập thất bại: " + vbb.ExceptionString));
                this.ResponseUrl = vbb.ResponseUrl;
                LoginSuccess = false;
            }
            this.PostFinish = true;
            vbb.ClearCookies();
            vbb = null;
            return null;
        }

        public object CheckingBoxDied(int index)
        {
            this.OnProgressChanged(new ProgressChangedArgs(index, "Checking box died: " + this.Url + ": started"));
            VBB vbb = new VBB(this.Url);
            vbb.ProgressChanged += vbb_OnProcess;
            vbb.Index = index;
            if (vbb.IsBoxDied(this.Url))
            {
                IsDeadBox = true;
                this.OnProgressChanged(new ProgressChangedArgs(index, "Checking box died: done - result: true"));
            }
            else
            {
                IsDeadBox = false;
                this.OnProgressChanged(new ProgressChangedArgs(index, "Checking box died: done - result: false"));
            }
            return null;
        }

        public object StartPost(int index)
        {
            try
            {
                GlobalVariables.Logger.Info("Post link " + BoxLink + " belong to forum " + this.Url + ": started");
                GlobalVariables.Logger.Info("Login to box " + BoxLink + ": started");
                this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập diễn đàn"));

                VBB vbb = new VBB(this.Url);
                vbb.ProgressChanged += vbb_OnProcess;
                vbb.Index = index;
                vbb.StrRespond = string.Empty;

                if (vbb.Login(this.User, this.Pass))
                {
                    GlobalVariables.Logger.Info("Login to box " + BoxLink + ": done");
                    this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập thành công."));

                    // Concate message to generate back links
                    GlobalVariables.CreateBackLinks();
                    VerifyCapcha = false;

                    if (vbb.CreateNewThread(this.BoxLink, this.Subj, this.Mess + GlobalVariables.BackLinksGenerated))
                    {
                        this.ResponseUrl = vbb.ResponseUrl;
                        this.Completed = true;
                        this.PostFinish = true;
                        GlobalVariables.Logger.Info("Post new thread to box " + BoxLink + ": successfull");
                        this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng tin thành công."));
                    }
                    else
                    {
                        this.PostFinish = true;
                        if (vbb.StrRespond.ToLower().Contains("humanverify"))
                        {
                            StrRespond = vbb.StrRespond;
                            VerifyCapcha = true;
                            GlobalVariables.Logger.Warn("Post new thread to box " + BoxLink + ": failed - Verify Captcha!");
                            this.OnProgressChanged(new ProgressChangedArgs(index, "Chờ xác nhận  Captcha!"));
                        }
                        else
                        {
                            GlobalVariables.Logger.Error("Post new thread to box " + BoxLink + ": failed - error: " + vbb.ExceptionString);
                            this.OnProgressChanged(new ProgressChangedArgs(index, "Không thể đăng tin: " + vbb.ExceptionString));
                        }
                    }
                }
                else
                {
                    GlobalVariables.Logger.Error("Login to box " + BoxLink + ": failed - error: " + vbb.ExceptionString);
                    this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập thất bại: " + vbb.ExceptionString));
                }
                GlobalVariables.Logger.Info("Post link " + BoxLink + ": done");
                vbb.ClearCookies();
                vbb = null;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error("Post link " + BoxLink + ": failed - error: " + ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error("Post link " + BoxLink + ": failed - error: " + ex.Message);
                }
            }
            return null;
        }

        public object ReStartPost(int index)
        {
            this.OnProgressChanged(new ProgressChangedArgs(index, "Bắt đầu đăng..."));
            VBB vbb = new VBB(this.Url);
            vbb.ProgressChanged += vbb_OnProcess;
            vbb.Index = index;
            vbb.StrRespond = string.Empty;
            if (vbb.Login(this.User, this.Pass))
            {
                this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập thành công."));

                // Concate message to generate back links
                GlobalVariables.CreateBackLinks();
                VerifyCapcha = false;

                if (vbb.ReCreateNewThread(this.BoxLink, this.Subj, this.Mess + GlobalVariables.BackLinksGenerated, HumanVerification))
                {
                    this.ResponseUrl = vbb.ResponseUrl;
                    this.Completed = true;
                    this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng bài thành công."));
                }
                else
                {
                    if (vbb.StrRespond.ToLower().Contains("humanverify"))
                    {
                        StrRespond = vbb.StrRespond;
                        VerifyCapcha = true;
                        this.OnProgressChanged(new ProgressChangedArgs(index, "Xác nhận Capcha!"));
                    }
                    else
                    {
                        this.OnProgressChanged(new ProgressChangedArgs(index, "Không thể đăng bài: " + vbb.ExceptionString));
                    }
                }
            }
            else
            {
                this.OnProgressChanged(new ProgressChangedArgs(index, "Đăng nhập thất bại: " + vbb.ExceptionString));
            }
            vbb.ClearCookies();
            vbb = null;
            return null;
        }

        public object StartReply(int index)
        {
            this.OnProgressChanged(new ProgressChangedArgs(index, "Starting..."));
            VBB vbb = new VBB(this.Url);
            vbb.ProgressChanged += vbb_OnProcess;
            vbb.Index = index;
            if (vbb.Login(this.User, this.Pass))
            {
                this.OnProgressChanged(new ProgressChangedArgs(index, "Logged In."));
                if (vbb.CreateNewReply(this.ThreadLink, this.Mess))
                {
                    this.OnProgressChanged(new ProgressChangedArgs(index, "Completed."));
                    this.ResponseUrl = vbb.ResponseUrl;
                    this.Completed = true;
                }
                else
                {
                    this.OnProgressChanged(new ProgressChangedArgs(index, "Could not create new thread:" + vbb.ExceptionString));
                }
            }
            else
            {
                this.OnProgressChanged(new ProgressChangedArgs(index, "Login failed:" + vbb.ExceptionString));
            }
            vbb.ClearCookies();
            vbb = null;
            return null;
        }

        public override string ToString()
        {
            return this.Url;
        }

        private void vbb_OnProcess(object sender, EventArgs e)
        {
            var eventType = (ProgressChangedArgs)e;
            this.OnProgressChanged(eventType);
        }
    }
}
